package com.example.Controller;

import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.OmegaException;
import com.example.Result;
import com.example.ResultCodeEnum;
import com.example.Service.SysRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    @ApiOperation("全部")
    @GetMapping("/all")
    public Result list(){
        List<SysRole> list=sysRoleService.list();


return Result.build(list, ResultCodeEnum.SUCCESS);
    }
    @ApiOperation("分页")
    @GetMapping("/{page}/{limit}")
    public Result page(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> pages=new Page<>(page,limit);
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(sysRoleQueryVo.getRoleName())){
            lambdaQueryWrapper.like(SysRole::getRoleName,sysRoleQueryVo.getRoleName());
        }
        IPage<SysRole> page1=sysRoleService.page(pages,lambdaQueryWrapper);
        return Result.build(page1,ResultCodeEnum.SUCCESS);

    }
    @ApiOperation("新增")
    @PostMapping("/save")
    public Result add(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @ApiOperation("修改")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @ApiOperation("详情")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id){
        SysRole sysRole=sysRoleService.getById(id);
        return Result.build(sysRole,ResultCodeEnum.SUCCESS);
    }
    @ApiOperation("删除")
    @DeleteMapping("/remove/{id}")
    public Result shanchu(@PathVariable("id") Integer id){
        sysRoleService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @ApiOperation("获取所有角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable("userId") Integer userId) {
        Map<String, Object> map = sysRoleService.findRoleByUserId(userId);
        return Result.build(map, ResultCodeEnum.SUCCESS);

    }
    @ApiOperation("分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.build(null, ResultCodeEnum.SUCCESS);

    }
 }
